﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;
using System.Xml.Linq;

namespace Xmlparsing
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitDropDownList();
                DisplayAllMessages();
            }
        }


        protected void DisplayMessages(object sender, EventArgs e)
        {
            //selection.Controls.Add(new LiteralControl("<span>oOo</span>"));

            lock (this.GetType())
            {
                XDocument doc = XDocument.Load(Server.MapPath("~/Messages.xml"));

                var res = from messages in doc.Descendants("message")
                          where messages.Attribute("to").Value == uiDropDown.SelectedValue
                          select new Xmlparsing.Controls.XmlServerControl()
                          {
                              To = messages.Attribute("to").Value,
                              From = messages.Attribute("from").Value,
                              Title = messages.Element("title").Value,
                              Body = messages.Element("body").Value
                          };

                foreach (var currentMessage in res)
                {
                    form1.Controls.Add(currentMessage);
                }
            }            
        }


        public void DisplayAllMessages()
        {
            lock (this.GetType())
            {
                XDocument doc = XDocument.Load(Server.MapPath("~/Messages.xml"));

                var res = from messages in doc.Descendants("message")
                          select new Xmlparsing.Controls.XmlServerControl()
                          {
                              To = messages.Attribute("to").Value,
                              From = messages.Attribute("from").Value,
                              Title = messages.Element("title").Value,
                              Body = messages.Element("body").Value
                          };

                foreach (var currentMessage in res)
                {
                    form1.Controls.Add(currentMessage);
                }
            }
        }

        public void InitDropDownList()
        {
            XDocument doc = XDocument.Load(Server.MapPath("~/Messages.xml"));
            /*
            int[] numbers = { 1, 2, 3, 4, 5, 3, 6, 4, 7, 8, 9, 1, 0 };
            var nonRepeats = (from n in numbers select n).Distinct();
            */

            var res = (from messages in doc.Descendants("message")
                        select new ListItem()
                        {
                            Text = messages.Attribute("to").Value,
                            Value = messages.Attribute("to").Value
                        }).Distinct();

            foreach (var li in res)
            {
                uiDropDown.Items.Add(li);
            }
        }
    }
}